#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setFixedSize(640,480);

    //设置背景墙
    QPalette bgpal=palette();
    bgpal.setColor(QPalette::Background,QColor(0,0,0));
    setPalette (bgpal);
    connect(ui->jindutiao,SIGNAL(valueChanged(int)),this,SLOT(valueChanged(int)));
}

void MainWindow::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    int width=this->width();
    int height=this->height();
    int radius=((width>height)?height:width)/2;
    //移动画笔到中下方
    painter.translate(width/2,height*0.6);
    //启用反锯齿
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setPen(Qt::NoPen);
    //设置画刷颜色
    painter.setBrush(QColor(138,43,226));
    DrawPoint(painter,radius);
    DrawDigital(painter,radius-10);
    DrawCircle(painter,radius-35);
    DrawSmallScale(painter,radius-60);
    DrawBigScale(painter,radius-75);
    DrawText(painter,radius/2);
    DrawPointer(painter,radius-100);

}
//绘制外圈点
void MainWindow::DrawPoint(QPainter& painter,int radius)
{
    //组装点的路径图
    QPainterPath pointPath;
    pointPath.moveTo(-2,-2);
    pointPath.lineTo(2,-2);
    pointPath.lineTo(2,2);
    pointPath.lineTo(0,4);
    pointPath.lineTo(-2,2);
    //绘制13个小点
    for(int i=0;i<13;++i){
        QPointF point(0,0);
        painter.save();
        painter.setBrush(QColor(255,127,80));
        //计算并移动绘图对象中心点
        point.setX(radius*qCos(((210-i*20)*M_PI)/180));
        point.setY(radius*qSin(((210-i*20)*M_PI)/180));
        //计算并移动绘图对象的中心点
        painter.translate(point.x(),-point.y());
        //计算并选择绘图对象坐标
        painter.rotate(-120+i*20);
        //绘制路径
        painter.drawPath(pointPath);
        painter.restore();
    }
}
//绘制外圈数字，原理和绘制圆圈点一样
void MainWindow::DrawDigital(QPainter& painter,int radius)
{
    //设置画笔，画笔默认NOPEN
    painter.setPen(QColor(218,112,214));
    QFont font;
    font.setFamily("Cambria");
    font.setPointSize(15);
    painter.setFont(font);
    for(int i=0;i<13;++i){
        QPointF point(0,0);
        painter.save();
        point.setX(radius*qCos(((210-i*20)*M_PI)/180));
        point.setY(radius*qSin(((210-i*20)*M_PI)/180));
        painter.translate(point.x(),-point.y());
        painter.rotate(-120+i*20);
        painter.drawText(-15, 0, 30, 20,Qt::AlignCenter,QString::number(i*20));
        painter.restore();
    }
    //还原画笔
    painter.setPen(Qt::NoPen);
}
//绘制外圈
void MainWindow::DrawCircle(QPainter& painter,int radius)
{
    //保存绘图对象
    painter.save();
    //计算大小圆路径
    QPainterPath outRing;
    QPainterPath inRing;
    outRing.moveTo(0,0);
    inRing.moveTo(0,0);
    outRing.arcTo(-radius,-radius, 2*radius,2*radius,-31,242);
    inRing.addEllipse(-radius+20,-radius+20,2*(radius-20),2*(radius-20));
    outRing.closeSubpath();
    //设置渐变色k
    QRadialGradient radialGradient(0,0,radius,0,0);
    radialGradient.setColorAt(0.93,QColor(138,43,226));
    radialGradient.setColorAt(1,QColor(0,0,0));
    //设置画刷
    painter.setBrush(radialGradient);
    //大圆减小圆
    painter.drawPath(outRing.subtracted(inRing));
    //painter.drawPath(outRing);
    //painter.drawPath(inRing);
    painter.restore();
}
//绘制刻度
void MainWindow::DrawSmallScale(QPainter& painter,int radius)
{
    //组装点的路径图
    QPainterPath pointPath;
    pointPath.moveTo(-2,-2);
    pointPath.lineTo(-1,-4);
    pointPath.lineTo(1,-4);
    pointPath.lineTo(2,-2);
    pointPath.lineTo(1,8);
    pointPath.lineTo(-1,8);
    //绘制121个小点
    for(int i=0;i<121;++i){
        QPointF point(0,0);
        painter.save();
        point.setX(radius*qCos(((210-i*2)*M_PI)/180));
        point.setY(radius*qSin(((210-i*2)*M_PI)/180));
        painter.translate(point.x(),-point.y());
        painter.rotate(-120+i*2);
        if(i>=90) painter.setBrush(QColor(250,0,0));
        painter.drawPath(pointPath);
        painter.restore();
    }
}
//绘制刻度
void MainWindow::DrawBigScale(QPainter& painter,int radius)
{
    //组装点的路径图
    QPainterPath pointPath1;
    pointPath1.moveTo(-2,-2);
    pointPath1.lineTo(-1,-4);
    pointPath1.lineTo(1,-4);
    pointPath1.lineTo(2,-2);
    pointPath1.lineTo(1,8);
    pointPath1.lineTo(-1,8);
    QPainterPath pointPath2;
    pointPath2.moveTo(-2,-2);
    pointPath2.lineTo(-1,-4);
    pointPath2.lineTo(1,-4);
    pointPath2.lineTo(2,-2);
    pointPath2.lineTo(1,15);
    pointPath2.lineTo(-1,15);
    //绘制25个刻度
    for(int i=0;i<25;++i){
        QPointF point(0,0);
        painter.save();
        point.setX(radius*qCos(((210-i*10)*M_PI)/180));
        point.setY(radius*qSin(((210-i*10)*M_PI)/180));
        painter.translate(point.x(),-point.y());
        painter.rotate(-120+i*10);
        if(i>=18) painter.setBrush(QColor(250,0,0));
        if(i%2){
            painter.drawPath(pointPath1);
        }
        else{
            painter.drawPath(pointPath2);
        }
        painter.restore();
    }
}
//绘制中心文字km/h
void MainWindow::DrawText(QPainter& painter,int radius)
{
    painter.save();
    painter.setPen(QColor(153,51,250));
    QFont font;
    font.setFamily("Cambria");
    font.setPointSize(16);
    painter.setFont(font);
    painter.drawText(-25, -radius, 50, 20,Qt::AlignCenter,QString("km/h"));
    painter.restore();
}
//绘制指针
void MainWindow::DrawPointer(QPainter& painter,int radius)
{
    //组装点的路径图
    QPainterPath pointPath;
    pointPath.moveTo(10,0);
    pointPath.lineTo(1,-radius);
    pointPath.lineTo(-1,-radius);
    pointPath.lineTo(-10,0);
    pointPath.arcTo(-10,0,20,20,180,180);
    QPainterPath inRing;
    inRing.addEllipse(-5,-5,10,10);
    painter.save();
    QRadialGradient radialGradient(0,0,radius,0,0);
    radialGradient.setColorAt(0,QColor(0,199,140,150));
    radialGradient.setColorAt(1,QColor(255,153,18,150));
    //计算并选择绘图对象坐标
    painter.rotate(degRotate);
    painter.setBrush(radialGradient);
    painter.drawPath(pointPath.subtracted(inRing));
    painter.restore();
}
void MainWindow::valueChanged(int value)
{
    this->degRotate = value;
    update();
}
MainWindow::~MainWindow()
{
    delete ui;
}

